package com.hktf.moedokilock.wxapi;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.widget.Toast;

import com.hktf.corelib.eventBus.LoginEvent;
import com.hktf.corelib.utils.LogMds;
import com.hktf.uilib.utils.WxsdkUtil;
import com.tencent.mm.opensdk.constants.ConstantsAPI;
import com.tencent.mm.opensdk.modelbase.BaseReq;
import com.tencent.mm.opensdk.modelbase.BaseResp;
import com.tencent.mm.opensdk.modelbiz.SubscribeMessage;
import com.tencent.mm.opensdk.modelbiz.WXLaunchMiniProgram;
import com.tencent.mm.opensdk.modelbiz.WXOpenBusinessView;
import com.tencent.mm.opensdk.modelbiz.WXOpenBusinessWebview;
import com.tencent.mm.opensdk.modelmsg.SendAuth;
import com.tencent.mm.opensdk.openapi.IWXAPI;
import com.tencent.mm.opensdk.openapi.IWXAPIEventHandler;
import com.tencent.mm.opensdk.openapi.WXAPIFactory;

import org.greenrobot.eventbus.EventBus;

import java.lang.ref.WeakReference;

public class WXEntryActivity extends Activity implements IWXAPIEventHandler {
    private static String TAG = "MicroMsg.WXEntryActivity";

    private IWXAPI api;
    private MyHandler handler;

    private static class MyHandler extends Handler {
        private final WeakReference<WXEntryActivity> wxEntryActivityWeakReference;

        public MyHandler(WXEntryActivity wxEntryActivity) {
            wxEntryActivityWeakReference = new WeakReference<WXEntryActivity>(wxEntryActivity);
        }

        @Override
        public void handleMessage(Message msg) {
            int tag = msg.what;
            //			switch (tag) {
            //				case NetworkUtil.GET_TOKEN: {
            //					Bundle data = msg.getData();
            //					JSONObject json = null;
            //					try {
            //						json = new JSONObject(data.getString("result"));
            //						String openId, accessToken, refreshToken, scope;
            //						openId = json.getString("openid");
            //						accessToken = json.getString("access_token");
            //						refreshToken = json.getString("refresh_token");
            //						scope = json.getString("scope");
            //						Intent intent = new Intent(wxEntryActivityWeakReference.get(), SendToWXActivity.class);
            //						intent.putExtra("openId", openId);
            //						intent.putExtra("accessToken", accessToken);
            //						intent.putExtra("refreshToken", refreshToken);
            //						intent.putExtra("scope", scope);
            //						wxEntryActivityWeakReference.get().startActivity(intent);
            //					} catch (JSONException e) {
            //						Log.e(TAG, e.getMessage());
            //					}
            //				}
            //			}
        }
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        api = WXAPIFactory.createWXAPI(this, WxsdkUtil.APP_ID, false);
//        api.registerApp(appId);
        handler = new MyHandler(this);


        try {
            Intent intent = getIntent();
            api.handleIntent(intent, this);
        } catch (Exception e) {
            e.printStackTrace();
        }
        LogMds.w("cdz","==============WXEntryActivity onCreate=================");

//        final SendAuth.Req req = new SendAuth.Req();
//        req.scope = "snsapi_userinfo"; // 只能填 snsapi_userinfo
//        req.state = "wechat_sdk_demo_test";
//        api.sendReq(req);

//        WxsdkUtil.gotoWxlogin(getApplicationContext());

    }

    @Override
    protected void onNewIntent(Intent intent) {
        super.onNewIntent(intent);

        setIntent(intent);
        api.handleIntent(intent, this);
    }

    @Override
    public void onReq(BaseReq req) {
        switch (req.getType()) {
            case ConstantsAPI.COMMAND_GETMESSAGE_FROM_WX:
                break;
            case ConstantsAPI.COMMAND_SHOWMESSAGE_FROM_WX:
                break;
            default:
                break;
        }
        LogMds.w("cdz","req===================="+req.getType());
    }

    @Override
    public void onResp(BaseResp resp) {
        int result = 0;

        //		switch (resp.errCode) {
        //		case BaseResp.ErrCode.ERR_OK:
        //			result = R.string.errcode_success;
        //			break;
        //		case BaseResp.ErrCode.ERR_USER_CANCEL:
        //			result = R.string.errcode_cancel;
        //			break;
        //		case BaseResp.ErrCode.ERR_AUTH_DENIED:
        //			result = R.string.errcode_deny;
        //			break;
        //		case BaseResp.ErrCode.ERR_UNSUPPORT:
        //			result = R.string.errcode_unsupported;
        //			break;
        //		default:
        //			result = R.string.errcode_unknown;
        //			break;
        //		}

        LogMds.w("cdz", "onResp type=" + resp.getType());


        if (resp.getType() == ConstantsAPI.COMMAND_SUBSCRIBE_MESSAGE) {
            SubscribeMessage.Resp subscribeMsgResp = (SubscribeMessage.Resp) resp;
            String text = String.format("openid=%s\ntemplate_id=%s\nscene=%d\naction=%s\nreserved=%s",
                    subscribeMsgResp.openId, subscribeMsgResp.templateID, subscribeMsgResp.scene, subscribeMsgResp.action, subscribeMsgResp.reserved);

            Toast.makeText(this, text, Toast.LENGTH_LONG).show();
        }

        if (resp.getType() == ConstantsAPI.COMMAND_LAUNCH_WX_MINIPROGRAM) {
            WXLaunchMiniProgram.Resp launchMiniProgramResp = (WXLaunchMiniProgram.Resp) resp;
            String text = String.format("openid=%s\nextMsg=%s\nerrStr=%s",
                    launchMiniProgramResp.openId, launchMiniProgramResp.extMsg, launchMiniProgramResp.errStr);

            Toast.makeText(this, text, Toast.LENGTH_LONG).show();
        }

        if (resp.getType() == ConstantsAPI.COMMAND_OPEN_BUSINESS_VIEW) {
            WXOpenBusinessView.Resp launchMiniProgramResp = (WXOpenBusinessView.Resp) resp;
            String text = String.format("openid=%s\nextMsg=%s\nerrStr=%s\nbusinessType=%s",
                    launchMiniProgramResp.openId, launchMiniProgramResp.extMsg, launchMiniProgramResp.errStr, launchMiniProgramResp.businessType);

            Toast.makeText(this, text, Toast.LENGTH_LONG).show();
        }

        if (resp.getType() == ConstantsAPI.COMMAND_OPEN_BUSINESS_WEBVIEW) {
            WXOpenBusinessWebview.Resp response = (WXOpenBusinessWebview.Resp) resp;
            String text = String.format("businessType=%d\nresultInfo=%s\nret=%d", response.businessType, response.resultInfo, response.errCode);

            Toast.makeText(this, text, Toast.LENGTH_LONG).show();
        }

        if (resp.getType() == ConstantsAPI.COMMAND_SENDAUTH) {
            SendAuth.Resp authResp = (SendAuth.Resp) resp;
            final String code = authResp.code;
            LogMds.e("cdz", "errcode=" + resp.errCode);
            LogMds.e("cdz", "code=" + code);
            //			NetworkUtil.sendWxAPI(handler, String.format("https://api.weixin.qq.com/sns/oauth2/access_token?" +
            //							"appid=%s&secret=%s&code=%s&grant_type=authorization_code", "wxd930ea5d5a258f4f",
            //					"1d6d1d57a3dd063b36d917bc0b44d964", code), NetworkUtil.GET_TOKEN);

            switch (resp.errCode) {
                case BaseResp.ErrCode.ERR_OK:
//                    Toast.makeText(this, "授权成功，code: " + code, Toast.LENGTH_SHORT).show();
                    EventBus.getDefault().post(new LoginEvent(LoginEvent.GET_CODE_SUCCESS,code));
                    // 这里可以使用 code 去换取 access_token 等信息
                    break;
                case BaseResp.ErrCode.ERR_USER_CANCEL:
                    Toast.makeText(this, "用户取消授权", Toast.LENGTH_SHORT).show();
                    break;
                case BaseResp.ErrCode.ERR_AUTH_DENIED:
                    Toast.makeText(this, "授权被拒绝", Toast.LENGTH_SHORT).show();
                    break;
                default:
                    Toast.makeText(this, "授权失败，错误码: " + resp.errCode, Toast.LENGTH_SHORT).show();
                    break;

            }
            finish();
        }
    }
}